package com.yxt.ui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.yxt.common.sys.DateUtil;
import com.yxt.common.sys.StringUtil;
import com.yxt.common.ui.ActivityStackControlUtil;
import com.yxt.common.widget.ListWheelAdapter;
import com.yxt.common.widget.NumericWheelAdapter;
import com.yxt.common.widget.WheelView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button; 
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 雨情统计
 * @author crazyman
 */
public class RainIntervalActivity extends Activity {
	private TextView lblStartTime;
	private TextView lblEndTime;
	private List<String> displayDays = new ArrayList<String>();
	private EditText txtDrp;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.rain_interval);   //软件activity的布局
		ActivityStackControlUtil.add(this);	
		//初始化统计
		initComponent();
	}
	
	private void initComponent(){
		fillDisplayDays();
		
		Button btnBackHome = (Button)findViewById(R.id.btnBackHome);
		Button btnInterval = (Button)findViewById(R.id.btnInterval);
		Button btnTime = (Button)findViewById(R.id.btnTime);
		lblStartTime = (TextView)findViewById(R.id.lblStartTime);
		lblEndTime = (TextView)findViewById(R.id.lblEndTime);
		txtDrp = (EditText)findViewById(R.id.txtInterval);
		btnBackHome.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});
		
		final Calendar calendar = Calendar.getInstance();
		final String date = calendar.get(Calendar.YEAR) + "年 " + String.valueOf(calendar.get(Calendar.MONTH)+1) + "月" + calendar.get(Calendar.DATE) + "日 " + calendar.get(Calendar.HOUR_OF_DAY) +":00";
		lblStartTime.setText(date);
		lblEndTime.setText(date);
		
		btnTime.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				lblStartTime.setText(date);
				lblEndTime.setText(date);
				txtDrp.setText("");
			}
		});
		
		btnInterval.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				String drp = txtDrp.getText().toString();
				if (StringUtil.isEmpty(drp)) {
					Toast.makeText(getApplicationContext(), "毫米数不能为空", Toast.LENGTH_LONG).show();
					return;
				} else {
					Intent intent = new Intent();
					intent.putExtra("starttime", lblStartTime.getText());
					intent.putExtra("endtime", lblEndTime.getText());
					intent.putExtra("drp", txtDrp.getText().toString());
					intent.setClass(getApplicationContext(), RainIntervalCoutActivity.class);
					startActivity(intent);
				}
			}
		});
		
		lblStartTime.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				View view = View.inflate(RainIntervalActivity.this, R.layout.comm_interval, null);
				final AlertDialog menuDialog = new AlertDialog.Builder(RainIntervalActivity.this).create();
				menuDialog.setView(view);
				menuDialog.show();
				final WheelView wheelDay = (WheelView)view.findViewById(R.id.wheelDay);
				final WheelView wheelHour = (WheelView)view.findViewById(R.id.wheelHour);
				wheelDay.setAdapter(new ListWheelAdapter<String>(displayDays));
				wheelHour.setAdapter(new NumericWheelAdapter(0, 23));
				wheelHour.setLabel("时");
				Button btnConfirm = (Button)view.findViewById(R.id.btnConfirm);
				Button btnCancel = (Button)view.findViewById(R.id.btnCancel);
				btnConfirm.setOnClickListener(new OnClickListener(){
					public void onClick(View arg0) {
						String text = calendar.get(Calendar.YEAR) + "年 " + displayDays.get(wheelDay.getCurrentItem()) + " "  + String.valueOf(wheelHour.getCurrentItem()) + "时";
						lblStartTime.setText(text);
						menuDialog.cancel();
					}
				});
				btnCancel.setOnClickListener(new OnClickListener(){
					public void onClick(View arg0) {
						menuDialog.cancel();
					}
				});
			}
		});
		lblEndTime.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				View view = View.inflate(RainIntervalActivity.this, R.layout.comm_interval, null);
				final AlertDialog menuDialog = new AlertDialog.Builder(RainIntervalActivity.this).create();
				menuDialog.setView(view);
				menuDialog.show();
				final WheelView wheelDay = (WheelView)view.findViewById(R.id.wheelDay);
				final WheelView wheelHour = (WheelView)view.findViewById(R.id.wheelHour);
				wheelDay.setAdapter(new ListWheelAdapter<String>(displayDays));
				wheelHour.setAdapter(new NumericWheelAdapter(0, 23));
				wheelHour.setLabel("时");
				Button btnConfirm = (Button)view.findViewById(R.id.btnConfirm);
				Button btnCancel = (Button)view.findViewById(R.id.btnCancel);
				btnConfirm.setOnClickListener(new OnClickListener(){
					public void onClick(View arg0) {
						String text = calendar.get(Calendar.YEAR) + "年 " + displayDays.get(wheelDay.getCurrentItem()) + " "  + String.valueOf(wheelHour.getCurrentItem()) + "时";
						lblEndTime.setText(text);
						menuDialog.cancel();
					}
				});
				btnCancel.setOnClickListener(new OnClickListener(){
					public void onClick(View arg0) {
						menuDialog.cancel();
					}
				});
			}
		});
	}
	
	
	private void fillDisplayDays() {
		for (int i = 6; i > 0; i--) {
			Date now = new Date();
			now.setDate(now.getDate()-i);
			displayDays.add(DateUtil.getIntervalDays(now));
		}
		displayDays.add("今天");
	}
	
	protected void onDestroy() {     
        super.onDestroy();
        ActivityStackControlUtil.remove(this);
	}
}